package com.gbx.ximalayating.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.callback.OnImageViewClickListener;
import com.gbx.ximalayating.entity.CategoriesEntity;

import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */

public class BroadcastAdapter extends BaseAdapter implements View.OnClickListener {

    private final Context context;
    private final List<CategoriesEntity> categories;
    private final List<CategoriesEntity> data;
    private OnImageViewClickListener listener;
    private final LayoutInflater inflater;

    public BroadcastAdapter(Context context, List<CategoriesEntity> categories, List<CategoriesEntity> data, OnImageViewClickListener listener) {
        this.context = context;
        this.categories = categories;
        this.data = data;
        this.listener = listener;
        inflater = LayoutInflater.from(context);
    }

    public BroadcastAdapter(Context context, List<CategoriesEntity> categories, List<CategoriesEntity> data) {
        this.context = context;
        this.categories = categories;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return categories.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;{
            if (position != categories.size()-1){
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.item_broadcast,parent,false);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();

                }
                holder.itemTV.setText(categories.get(position).getName());
                return convertView;
            }else {
                if (categories.size()==8){
                    convertView = inflater.inflate(R.layout.item_pop,parent,false);
                    holder = new ViewHolder(convertView);
                    holder.popIV.setOnClickListener(this);
                    return convertView;
                }else if (categories.size()==16){
                    convertView = inflater.inflate(R.layout.item_stop,parent,false);
                    holder = new ViewHolder(convertView);
                    holder.stopIV.setOnClickListener(this);
                    return convertView;
                }else {
                    return convertView;
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pop_broadcast_iv:
                categories.clear();
                categories.addAll(data);
                categories.add(data.get(0));
                Log.i("1471","全部数量"+categories.size());
                listener.onImageViewClick(categories);
                break;
            case R.id.stop_broadcast_iv:
                categories.clear();
                for (int i = 0; i < 8; i++) {
                    categories.add(i,data.get(i));
                }
                Log.i("1471","数量"+categories.size());
                listener.onImageViewClick(categories);
                break;
        }
    }

    private static class ViewHolder {
        private final TextView itemTV;
        private final ImageButton popIV, stopIV;

        public ViewHolder(View convertView) {
            super();
            itemTV = ((TextView) convertView.findViewById(R.id.item_broadcast_tv));
            popIV = ((ImageButton) convertView.findViewById(R.id.pop_broadcast_iv));
            stopIV = ((ImageButton) convertView.findViewById(R.id.stop_broadcast_iv));

        }
    }


}
